/**
 * Created by Neville Kadwa.
 */
package org.shiftone.cdep;

import org.apache.log4j.Logger;
import org.shiftone.cdep.model.PackageInfo;

import java.util.HashMap;
import java.util.HashSet;

public class PackageManager
{
    public static final Logger LOG = Logger.getLogger(PackageManager.class);

    private HashSet rootSet = new HashSet();
    private HashMap packageMap = new HashMap();

    public PackageManager()
    {
        PackageInfo rootPackage = new PackageInfo();
        rootPackage.setPackageName("");
        packageMap.put("", rootPackage);
    }

    private String getPackageName(String className)
    {
        int loc = className.lastIndexOf('/');
        if (loc == -1)
            return("");
        return className.substring(0, loc).toLowerCase();
    }

    public PackageInfo addEntry(String className)
    {
        if (className == null)
            return null;

        String packageName = getPackageName(className);
        if (packageName.length() < 1)
            return null;
        PackageInfo currentPackage = (PackageInfo) packageMap.get(packageName);
        if (currentPackage != null)
            return currentPackage;

        currentPackage = new PackageInfo();
        currentPackage.setPackageName(packageName);
        PackageInfo parentPackage = null;

        int loc = packageName.indexOf('/');
        while (loc != -1)
        {
            String entry = packageName.substring(0, loc);

            PackageInfo lastPackage = parentPackage;

            parentPackage = (PackageInfo) packageMap.get(entry);
            if (parentPackage == null)
            {
                parentPackage = new PackageInfo();
                parentPackage.setPackageName(entry);
                packageMap.put(entry, parentPackage);

                if (lastPackage != null)
                {
                    lastPackage.addChild(parentPackage);
                } else {
                    rootSet.add(parentPackage);
                }
            }

            loc = packageName.indexOf('/', loc+1);
        }

        if (parentPackage == null)
            rootSet.add(currentPackage);
        if (parentPackage != null)
            parentPackage.addChild(currentPackage);
        packageMap.put(packageName, currentPackage);

        return currentPackage;
    }

    public PackageInfo getPackage(String className)
    {
        String packageName = getPackageName(className);

        return (PackageInfo) packageMap.get(packageName);
    }

    public Integer getPackageId(String className)
    {
        PackageInfo packageInfo = getPackage(className);
        if ((packageInfo == null) || (packageInfo.getPackageId() == 0))
            return null;
        return new Integer(packageInfo.getPackageId());
    }

    public PackageInfo[] getRootPackages()
    {
        return (PackageInfo[]) rootSet.toArray(new PackageInfo[rootSet.size()]);
    }

}
